package com.tliaswebmanagement.controller;

import com.tliaswebmanagement.pojo.Dept;
import com.tliaswebmanagement.pojo.Result;
import com.tliaswebmanagement.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.FileNotFoundException;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 查询所有部门
     *
     * @return
     */
    @GetMapping
    public Result list() {

        List<Dept> list = deptService.list();
        return Result.success(list);
    }

    /**
     * 根据id删除部门
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) throws FileNotFoundException {
        log.info("删除的是 {}", id);
        deptService.delete(id);
        return Result.success();
    }

    /**
     * 添加部门
     * @return
     */
    @PostMapping
    public Result add ( @RequestBody Dept dept) {
        deptService.add( dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id ){
       Dept dept = deptService.getById(id);
       return Result.success(dept);
    }

    @PutMapping
    public Result update( @RequestBody Dept dept ){
        deptService.update(dept);
        return  Result.success();
    }

}
